# Contributor: TBK <alpine@jjtc.eu>
# Maintainer: TBK <alpine@jjtc.eu>
pkgname=commons-daemon
pkgver=1.2.2
pkgrel=0
pkgdesc="Tools to allow Java programs to run as UNIX daemons."
url="https://commons.apache.org/daemon/"
arch="all !mips !mips64"
license="Apache-2.0"
options="!check" # No test suite
depends="openjdk8-jre-base"
makedepends="docbook2x libcap-dev linux-headers maven openjdk8" # update --with-java when bumping openjdk
subpackages="$pkgname-doc jsvc:_jsvc jsvc-doc:_jsvc_doc"
source="https://mirrors.dotsrc.org/apache/commons/daemon/source/commons-daemon-$pkgver-src.tar.gz
	rat_workaround.patch
	"
builddir="$srcdir/$pkgname-$pkgver-src"

build() {
	# commons-daemon.jar
	mvn install -DskipTests=true -Dmaven.javadoc.skip=false -B

	# jsvc
	cd "$builddir/src/native/unix"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--with-java=/usr/lib/jvm/java-1.8-openjdk \
		CFLAGS="$CFLAGS -I/usr/lib/jvm/java-1.8-openjdk/include/linux"
	make

	# jsvc manpage
	db2x_xsltproc -s man man/jsvc.1.xml -o jsvc.1
	gzip -9 jsvc.1
}

package() {
	install -Dm644 target/$pkgname-$pkgver.jar \
		"$pkgdir"/usr/share/java/$pkgname-$pkgver.jar
	ln -s $pkgname-$pkgver.jar "$pkgdir"/usr/share/java/$pkgname.jar
}

doc() {
	default_doc

	mkdir -p "$subpkgdir"/usr/share/doc/$pkgname
	cp -a "$builddir"/src/docs/* "$subpkgdir"/usr/share/doc/$pkgname/
	cp -a "$builddir"/src/samples "$subpkgdir"/usr/share/doc/$pkgname/
}

_jsvc() {
	pkgdesc="Executes classfile that implements a Daemon interface."
	url="https://commons.apache.org/proper/commons-daemon/jsvc.html"
	depends="$pkgname libcap"

	cd "$builddir/src/native/unix"
	install -Dm755 jsvc "$subpkgdir"/usr/bin/jsvc
}

_jsvc_doc() {
	cd "$builddir/src/native/unix"
	install -Dm644 jsvc.1.gz "$subpkgdir"/usr/share/man/man1/jsvc.1.gz
}

sha512sums="aea5e25f1152be2f8855e98191d243daff97e02eb0dcb72a0ca681c2ec42f42dda038af3496e2fc9623d6d9982d63e4403149ee231745f42605a7a575ebc8cc3  commons-daemon-1.2.2-src.tar.gz
1ede66e199e82bb788280159be238fba5d1e405c6f01b8123486397f5cc010b48df023c6a799a53de3e09c128a69842c4c2ff5adf735f0ae253ccec4eac28a5d  rat_workaround.patch"
